#!/usr/bin/env python
# coding: utf-8

import cv2
import numpy as np

get_ipython().set_next_input('from copy import deepcopy');
get_ipython().run_line_magic('pinfo', 'deepcopy')

#捕获视频
cap = cv2.VideoCapture('siderwalk.mp4')

print (cap)

frameNum = 0
while (cap.isOpened()):
    ret, frame = cap.read()
    frameNum += 1
    if ret == True:
        tempframe = deepcopy(frame.copy())
        if (frameNum == 1):
            previousframe = cv2.cvtColor(tempframe, cv2.COLOR_BGR2GRAY)
        if (frameNum > 2):
            currentframe = cv2.cvtColor(tempframe, cv2.COLOR_BGR2GRAY)
            currentframe = cv2.absdiff(currentframe, previousframe)

            ret, threshold_frame = cv2.threshold(currentframe, 40, 255, cv2.THRESH_BINARY)
            gaussian_frame = cv2.GaussianBlur(threshold_frame, (3, 3), 0)

            contours, _ = cv2.findContours(gaussian_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            for c in contours:
                if cv2.contourArea(c) < 300:
                    continue
                (x, y, w, h) = cv2.boundingRect(c)
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)

            cv2.imshow('frame', frame)
            cv2.imshow('ABSDIFF', currentframe)
            cv2.imshow('BINARY', threshold_frame)
            cv2.imshow('Gaussian', gaussian_frame)
            if cv2.waitKey(33) & 0xFF == ord('q'):
                break
        previousframe = cv2.cvtColor(tempframe, cv2.COLOR_BGR2GRAY)
cap.release()
cv2.destroyAllWindows()



